import axios from 'axios'

// 单个实例
const instance = axios.create()

// 拦截器
// 请求
instance.interceptors.request.use(
  config => {
    config.timeout = 10000
    // 环境判断
    config.baseURL = process.env.NODE_ENV == 'development' ? '' : 'http://jx.1314000.cn:9000/'

    return config
  },
  err => Promise.reject(err)
)

// 响应
instance.interceptors.response.use(
  res => res.data,
  err => Promise.reject(err)
)

// 封装 get/post/put/delete请求动作方法
/**
 * get请求
 * @param {string} url 请求url地址
 * @param {object} config 请求头添加额外的头信息
 * @returns Promise
 */
export const get = (url, config = {}) => instance.get(url, config)

/**
 * post请求
 * @param {string} url 请求url地址
 * @param {object} data 请求体数据
 * @param {object} config 请求头额外信息
 * @returns Promise
 */
export const post = (url, data = {}, config = {}) => instance.post(url, data, config)

export const put = (url, data = {}, config = {}) => instance.put(url, data, config)
export const del = (url, data = {}, config = {}) => instance.delete(url, data, config)
